package com.ruoyi.blog.controller;

import com.ruoyi.blog.domain.BlogCategory;
import com.ruoyi.blog.service.IBlogCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 超级管理员专属Controller
 */
@RestController
@RequestMapping("/blog/admin/category")
public class BlogCategoryController extends BaseController {
    @Autowired
    IBlogCategoryService categoryService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<BlogCategory> getAllCategories() {
        return categoryService.getAllCategories();
    }

    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    public AjaxResult deleteById(@PathVariable String ids) {
        boolean result = categoryService.deleteCategoryByIds(ids);
        if (result) {
            return success("删除成功!");
        }
        return error("删除失败!");
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public AjaxResult addNewCate(BlogCategory category) {

        if ("".equals(category.getCateName()) || category.getCateName() == null) {
            return error("请输入栏目名称!");
        }

        int result = categoryService.addCategory(category);

        if (result == 1) {
            return success("添加成功!");
        }
        return error("添加失败!");
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public AjaxResult updateCate(BlogCategory category) {
        int i = categoryService.updateCategoryById(category);
        if (i == 1) {
            return success("修改成功!");
        }
        return error("修改失败!");
    }
}
